package com.efast.cafe.portal.controller.api;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.enums.ResultStatus;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.portal.bean.api.PortalApiViewBean;
import com.efast.cafe.portal.bean.api.PortalApiViewQueryBean;
import com.efast.cafe.portal.entity.api.PortalApiView;
import com.efast.cafe.portal.service.api.IPortalApiViewService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContext;

@Controller
@RequestMapping("/frame_center/api")
public class PortalApiViewController extends AbstractController {

	@Autowired
	private IPortalApiViewService portalApiViewService;

	/**
	 * 新增View
	 * 
	 * @Date : 2017年8月12日
	 * @param portalApiViewBean
	 * @param request
	 * @return
	 */
	@RequestMapping("/view/add")
	public @ResponseBody ResultBean insertApiView(@RequestBody PortalApiViewBean portalApiView,
			HttpServletRequest request) {
		ResultBean result = new ResultBean();
		result.setSuccess(true);
		try {
			portalApiViewService.insertApiView(portalApiView);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setMessage(e.getMessage());
			} else {
				// result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}
		result.setObject(portalApiView);
		return result;
	}

	/**
	 * 修改View
	 * 
	 * @Date : 2017年8月12日
	 * @param portalApiViewBean
	 * @param request
	 * @return
	 */
	@RequestMapping("/view/update")
	public @ResponseBody ResultBean updateApiView(@RequestBody PortalApiView portalApiView,
			HttpServletRequest request) {
	//	RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean();
		result.setSuccess(true);
		try {
			portalApiViewService.updateApiView(portalApiView);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setMessage(e.getMessage());
			} else {
				// result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}
		return result;
	}

	/**
	 * 修改View
	 * 
	 * @Date : 2017年8月12日
	 * @param portalApiViewBean
	 * @param request
	 * @return
	 */
	@RequestMapping("/view/checkComponentView")
	public @ResponseBody Boolean checkComponentView(String viewCode, String componmetCode, HttpServletRequest request) {
		if (portalApiViewService.checkComponentView(viewCode, componmetCode)) {
			return true;
		}
		return false;
	}

	/**
	 * 删除View
	 * 
	 * @Date : 2017年8月12日
	 * @param portalApiViewBean
	 * @param request
	 * @return
	 */
	@RequestMapping("/view/delete")
	public @ResponseBody ResultBean deleteApiView(@RequestBody PortalApiView portalApiView,
			HttpServletRequest request) {
		//RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean();
		result.setSuccess(true);
		try {
			portalApiViewService.deleteApiView(portalApiView);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setMessage(e.getMessage());
			} else {
				// result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}
		return result;
	}

	/**
	 * 查询View基本信息
	 * 
	 * @Date : 2017年8月14日
	 * @param portalApiViewBean
	 * @param request
	 * @return
	 */
	@RequestMapping("/view/detail")
	public @ResponseBody ResultBean queryApiViewByViewId(@RequestBody PortalApiView portalApiView,
			HttpServletRequest request) {
		ResultBean result = new ResultBean();
		result.setSuccess(true);
		try {
			portalApiView = portalApiViewService.queryApiViewByViewId(portalApiView);
			result.setObject(portalApiView);
		} catch (Exception e) {
			result.setSuccess(false);
			e.printStackTrace();
			if (e instanceof ServiceException) {
				result.setMessage(e.getMessage());
			} else {
				// result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}
		return result;
	}

	/**
	 * 查询View基本信息
	 * 
	 * @Date : 2017年8月14日
	 * @param portalApiViewBean
	 * @param request
	 * @return
	 */
	@RequestMapping("/view/queryViewByModuleId")
	public @ResponseBody PortalApiViewQueryBean queryViewByModuleId(@RequestBody PortalApiViewQueryBean queryBean,
			HttpServletRequest request) {
		return portalApiViewService.queryViewByModuleId(queryBean);
	}
	/**
	 * 查询指定模块下的views列表
	 *@param map
	 *@param request
	 *@return 
	 *ResultBean
	 *@Author  Administrator
	 *@Date 2017年12月25日
	 */
	@RequestMapping("/view/selectViewsListByMouduleId")
	public @ResponseBody ResultBean selectViewsListByMouduleId(@RequestBody Map<String,String> map,
			HttpServletRequest request) {
		ResultBean result = new ResultBean();
		result.setSuccess(true);
		Integer moduleId = null;
		if (map.containsKey("moduleId")) {
			moduleId = Integer.valueOf(map.get("moduleId"));
		}
		String code = null;
		if (map.containsKey("code")) {
			code =(String)map.get("code");
		}
		String i18nName = null;
		if (map.containsKey("i18nName")) {
			i18nName =(String)map.get("i18nName");
		}
		 try {
			 result.setObject(portalApiViewService.selectViewsListByMouduleId(moduleId,code,i18nName));
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
		}
		 return result;
	}
	
	/**
	 * 查询View 通过ViewCode 
	 * @Date        :      2017年9月25日
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/viewInfo")
	public @ResponseBody ResultBean queryByViewCode(@RequestBody Map<String, Object> map){
		ResultBean res = new ResultBean(true,null);
		try{
			Object object = portalApiViewService.queryByViewCode(map.get("viewCode").toString()); 
			if(object!=null){
				res.setObject(object);
			}else{
				res.setSuccess(false);
			}
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
}
